package org.esprit.ocm.server.metier.oca.impl;

import java.util.ArrayList;
import java.util.List;

import org.esprit.ocm.client.rpc.oca.ClusterOcaService;
import org.esprit.ocm.model.oca.ClusterOca;
import org.esprit.ocm.server.metier.oca.api.Client;
import org.esprit.ocm.server.metier.oca.api.OneResponse;
import org.esprit.ocm.server.metier.oca.api.cluster.Cluster;
import org.esprit.ocm.server.metier.oca.api.cluster.ClusterPool;

public class ClusterOcaServiceImpl implements ClusterOcaService {

	/**
	 * List All Cluster
	 * 
	 * @param client
	 * @return
	 * @throws Exception
	 */
	public List<ClusterOca> listAll(String secret, String endpoint) throws Exception {
		Client client = new Client(secret, endpoint);
		ClusterPool pool = new ClusterPool(client);
		pool.info();

		List<ClusterOca> l = new ArrayList<ClusterOca>();

		for (Cluster from : pool) {
			ClusterOca to = new ClusterOca();

			to.setId(from.xpath("ID"));
			to.setName(from.xpath("NAME"));
			to.setErrorMSG(from.info().getErrorMessage());

			l.add(to);
		}
		return l;
	}

	/**
	 * Describe Cluster
	 * 
	 * @param client
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public ClusterOca info(String secret, String endpoint, int id) throws Exception {
		Client client = new Client(secret, endpoint);
		Cluster from = new Cluster(id, client);
		
		ClusterOca to = new ClusterOca();
		to.setId(from.xpath("ID"));
		to.setName(from.xpath("NAME"));
		to.setErrorMSG(from.info().getErrorMessage());
		return to;
	}

	/**
	 * Allocate new Cluster
	 * 
	 * @param client
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public String allocate(String secret, String endpoint, String name) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = Cluster.allocate(client, name);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * Delete a Cluster
	 * 
	 * @param client
	 * @param hostId
	 * @throws Exception
	 */
	public String delete(String secret, String endpoint, int clusterId) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = Cluster.delete(client, clusterId);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * Add Host to cluster
	 * 
	 * @param client
	 * @param clusterId
	 * @param host
	 * @throws Exception
	 */
	public String addHost(String secret, String endpoint, int clusterId, int hostId) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = Cluster.add(client, clusterId, hostId);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * Reomve host from Cluster
	 * 
	 * @param client
	 * @param clusterId
	 * @param host
	 * @throws Exception
	 */
	public String removeHost(String secret, String endpoint, int hostId) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = Cluster.remove(client, hostId);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

}
